/*==============================================================================
 * AUTH: Matthew Baum, 9753095
 * DATE: February-11-2012
 * EDIT: March-03-2012
 * DESC: Represents the main TaskPro Task management solution.
 *==============================================================================*/

package SOEN_Phase_2;

public class TaskPro {

    // Task Table instance
    private TaskTable taskTable;

    // Person Table instance
    private PersonTable personTable;

    // Task Table XML PATH
    private String taskXMLPath = "tasks.xml";

    // Person Table XML PATH
    private String personXMLPath = "people.xml";

    // Constructor
    public TaskPro()
    {
        // Initialize the solution
        initialize();
    }

    // GET task table
    public TaskTable getTaskTable()
    {
        return taskTable;
    }

    // GET person table
    public PersonTable getPersonTable()
    {
        return personTable;
    }
    
    public String getPersonXMLPath()
    {
    	return personXMLPath;
    }
    
    public String getTaskXMLPath()
    {
    	return taskXMLPath;
    }

    // Initialization Routine
    public final void initialize()
    {
        // Initialize the task table
        initializeTaskTable( taskXMLPath );

        // Initialize the Person table
        initializePersonTable();
    }

    // Closing/Writing Routine
    public void saveAndClose()
    {
        // Run file saving routines
        saveTables();

        // Exit
        System.exit(0);
    }

    // Load Task Data From File
    private void initializeTaskTable(String taskTableFileName)
    {
        // Init Task Table Object
        taskTable = new TaskTable(taskTableFileName);
    }

    // Init the Person Table
    private void initializePersonTable()
    {
        // Init the Person table
        personTable = new PersonTable( taskTable );
    }

    // File saving routine
    private void saveTables()
    {
        // Save the Task Table to XML
        saveTaskTableToXML();

        // Save the Person table to XML
        writePersonTableToXML();
    }
    
    // Save Task Table to XML
    private void saveTaskTableToXML()
    {
        taskTable.saveToXML(taskXMLPath);
    }

    // Write Person Table to XML
    private void writePersonTableToXML()
    {
        personTable.writeToXML( personXMLPath );
    }

    // Write the Task table to a text file
    public void writeTaskTableToTXT(String dir, String fName)
    {
        // Write data to a TXT file
        taskTable.writeToTXT( dir, fName );
    }

    // Write the Person table to a text file
    public void writePersonTableToTXT(String dir, String fName)
    {
        // Write data to a TXT file
        personTable.writeToTXT( dir, fName );
    }

    // Modify the ID of a Task from the Task Table
    public boolean modifyTaskID(int ID, int assignID, int newID)
    {
    	boolean taskSuccess = false;
    	boolean personSuccess = false;
    	
    	taskSuccess = taskTable.modifyTaskID(ID, newID);
    	
    	// Only if the task ID was successfully changed should we update the person's assigned task id
    	if(taskSuccess)
    		personSuccess = personTable.modifyPersonsTaskID(assignID, ID, newID);
        return taskSuccess && personSuccess;
    }

    // Modify the title of a given task from the table
    public void modifyTaskTitle(int ID, String newTitle)
    {
        taskTable.modifyTaskTitle(ID, newTitle);
    }

    
    /*
     * Modify the employee a task is assigned to
     * Current behavior:
     * - Receive employee ID and NAME
     * - IF ID is NOT registered, create new employee and assign
     * - ELSE IF employee is registered, but different name,
     *   change name and assign.
     * - ELSE IF Registered and same name, assign.
     */
    public void modifyTaskAssignment(int taskID, int assigneeID, String assigneeName)
    {
        // Check that task exists, and get it
        Task task = taskTable.getTaskByID(taskID);

        // Proceed if the task exists
        if(task != null)
        {
            // Check to see if employee ID is registered
            Person person = personTable.getPersonByID(assigneeID);

            if(person != null)
            {
                // Check that name given matches, if not change the name
                if(! person.getName().equals(assigneeName))
                {
                    // Modify employee name
                    personTable.modifyAssigneeName(assigneeID, assigneeName);
                }

                // Assign task to given employee
                personTable.assignTask(assigneeID, task);
            }

            // ELSE: NOT registered
            else
            {
                // 1- Register a new Person
                personTable.tryRegisterPerson(assigneeName, assigneeID);

                // 2- Assign a Task
                personTable.assignTask(assigneeID, task);
            }
        }
        
    }
    
    // Method to modify the assignee to a task
    public void modifyTaskAssignee(int taskID, String assigneeID)
    {
        taskTable.modifyAssignee(taskID, assigneeID);
    }

    // Modify a task's estimated duration
    public boolean modifyTaskEstimatedDuration(int ID, int duration)
    {
        // Success result
    	boolean taskSuccess = false;
        int assignID;
    	
        // Call method on task table (returns assignee ID)
        assignID = taskTable.modifyEstimatedDuration(ID, duration);

        // IF task modification succeeded, then modify person
        if(assignID != -1)
        {
            personTable.modifyPersonsTaskDuration(assignID);
            
            // Success
            taskSuccess = true;
        }
        
        return taskSuccess;
    }

    // Modify a task's completion status
    public void modifyTaskCompletion(int ID, boolean completion)
    {
        // Call method on task table
        taskTable.modifyCompletion(ID, completion);
    }

    // Update time invested on a task
    // NOTE: This will ADD the time passed (not SET) !
    public void modifyTaskTimeInvested(int ID, int timeToAdd)
    {
        // Call method on task table
        taskTable.modifyTimeInvested(ID, timeToAdd);
    }
    
    // This method updates the table after modification from UI
    public void updateTable()
    {
        int col = taskTable.getModifiedCol();
        int taskID = taskTable.getModifiedRowID();
        String value = taskTable.getNewValue();
        
        // Check which column is modified then do the modification
        switch (col)
        {
            // Title is modified
            case 1:
                modifyTaskTitle(taskID, value);
                break;                
                
            // Duration is modified
            case 2:
                modifyTaskEstimatedDuration(taskID, Integer.parseInt(value));
                break;
                
            // Assignee is modified   
            case 4:
            	modifyTaskAssignee(taskID, value);
                //modifyTaskAssignee(taskID, personTable.getPersonID(value), value);
                break;

            // Hours is modified 
            case 5:
                modifyTaskTimeInvested(taskID, Integer.parseInt(value));
                break; 
           
            // Completion is modified 
            case 6:
                modifyTaskCompletion(taskID, Boolean.parseBoolean(value));
                break;
                
            default:
                break;
        }
        
        personTable.initFromTaskTable(taskTable);
    }   
}
